<?php
/**
 * Copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author francissem
 * @copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */


class Core_System implements Core_Repository_Agent_Interface{


	private static $repos_pool;
	static function addRepository($name,Core_Repository_Abstract $repos){
		self::$repos_pool[$name]=$repos;
	}
	static function getRepository($name){
		return self::$repos_pool[$name];
	}
	static function getRepositories(){
		return self::$repos_pool;
	}


	static function dependPackage($package_name,$version_range){
		$curr_version=null;
		if(!preg_match("/[0-9\.]*(-[0-9\.]*)*/",$version_range)) throw new Core_System_Exception("Version Range Sytex Error");

		if(strtolower($package_name)=="zend"){
			$zend_version_location = __ROOT__."/library/Zend/Version.php";
			if(!file_exists($zend_version_location)) throw new Core_System_Exception("Package Not Found");
			require_once($zend_version_location);
			$curr_version = Zend_Version::VERSION;
		}else{
			$package_ini_file = __LIBRARY__."/{$package_name}/info.ini";
			if(!file_exists($package_ini_file)) throw new Core_System_Exception("Package Not Found");
			$info = parse_ini_file($package_ini_file);
			$curr_version = $info["version"];
		}
		$versions = split("-",$version_range);
		if(!isset($versions[1])) $versions[1]="9999.9999.9999";
		if($versions[0]==$versions[1] && !version_compare($curr_version,$versions[0],"=")){
			throw new Core_System_Exception_Version("Require package {$package_name} Ver. {$version_range}","package",$package_name,$version_range);
		}elseif(version_compare($curr_version,$versions[0],"<") || version_compare($curr_version,$versions[0],">")){
			throw new Core_System_Exception_Version("Require package {$package_name} Ver. {$version_range}","package",$package_name,$version_range);
		}
	}


	static $setting;
	static function initSetting($setting_arr){
		require_once(__LIBRARY__."/Core/Setting.php");
		self::$setting = new Core_Setting("system",$setting_arr);
	}
	static function getSetting($key=null,$subkey=null){
		if(isset($key) && isset($subkey)) return self::$setting[$key][$subkey];
		if(isset($key)) return self::$setting[$key];
		return self::$setting;
	}
	static function setSetting($key,$subkey,$value){
		self::$setting[$key][$subkey]=$value;
	}


	static function MakeDirectory($path){
		if(is_dir($path)) return;
		$path = preg_replace("/\\\+/","/",$path);
		if(!preg_match("/^".str_replace('/','\/',__DATA__)."/",$path)) throw new System_Exception("Target not in ".__DATA__);
		$path_node = split("/",$path);
		$curr_path = "";
		foreach($path_node as $dirname){
			$curr_path.="{$dirname}/";
			if(!is_dir($curr_path)){
				mkdir($curr_path);
			}
		}
	}

	static function RemoveFile($file){
		$file = preg_replace("/\\\+/","/",$file);
		if(!preg_match("/^".str_replace('/','\/',__DATA__)."/",$file)) throw new Core_System_Exception("Target not in ".__DATA__);
		if (is_dir($file) && !is_link($file)) {
			foreach(glob($file.'/*') as $sf) {
				if ( !self::RemoveFile($sf) ) return false;
			}
			return rmdir($file);
		} else {
			return unlink($file);
		}
	}
	static private function recursiveGetFileLastModifiedTime($path,$mtime){
		foreach(glob($path."/*") as $f){
			$t = filemtime($f);
			if($t>$mtime) $mtime=$t;
			if(is_dir($f)){
				self::recursiveGetFileLastModifiedTime($f,$mtime);
			}
		}
	}
	static function GetFileLastModifiedTime($path){
		$mtime=0;
		self::recursiveGetFileLastModifiedTime($path,&$mtime);
		return $mtime;
	}

}



?>